﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraEditors.Controls;
using Model;

namespace TanHungLong
{
    public partial class FrmSoQuyTienMat : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private DateTime tuNgay = DateTime.Now;
        private SoHieuTaiKhoanBOCollection soHieuTaiKhoanBOCollection = null;
        private string tenTaiKhoan = "";
        #endregion variant

        //=====================================
        #region form
        public FrmSoQuyTienMat()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmSoQuyTienMat_Load);
            this.Shown += new EventHandler(FrmSoQuyTienMat_Shown);
            this.KeyDown += new KeyEventHandler(FrmSoQuyTienMat_KeyDown);

            this.btnSearch.Click += new EventHandler(btnSearch_Click);
            this.btnPrint.Click += new EventHandler(btnPrint_Click);
            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.gridView.RowCellStyle += new RowCellStyleEventHandler(gridView_RowCellStyle);

            this.dtThang.Popup += new EventHandler(dtThang_Popup);
            this.lookUpTK.EditValueChanged += new EventHandler(lookUpTK_EditValueChanged);
        }

        private void FrmSoQuyTienMat_Shown(object sender, EventArgs e)
        {
            this.dtThang.Focus();
        }

        private void FrmSoQuyTienMat_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else
                if (this.btnRefresh.Visible && ((e.KeyCode == Keys.R && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F5)))
                    RefreshData();
                else
                    if (this.btnPrint.Visible && ((e.KeyCode == Keys.P && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F7)))
                        Print();
                    else
                        if ((e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F3))
                            FilterData();
        }

        private void FrmSoQuyTienMat_Load(object sender, EventArgs e)
        {
            DateTime dtNow = DateTime.Now;
            this.dtThang.DateTime = new DateTime(dtNow.Year, dtNow.Month, 1);

            Utility.InitControl(this.lookUpTK, "StrSoHieu", "StrSoHieu");
            MyStyle.SetGridStyle(this.gridView, false);

            RefreshData();
        }
        #endregion form

        //=====================================
        #region control
        private void dtThang_Popup(object sender, EventArgs e)
        {
            PopupDateEditForm form = (this.dtThang as IPopupControl).PopupWindow as PopupDateEditForm;
            form.Calendar.View = DateEditCalendarViewType.YearInfo;
        }

        private void lookUpTK_EditValueChanged(object sender, EventArgs e)
        {
            string taiKhoan = StaticUtil.GetString(this.lookUpTK.EditValue);
            this.tenTaiKhoan = "";
            this.lblTenTK.Text = "-";
            if (string.IsNullOrEmpty(taiKhoan) == false)
            {
                SoHieuTaiKhoanBO soHieuBO = this.soHieuTaiKhoanBOCollection.GetSoHieuTaiKhoanBO(taiKhoan);
                if (soHieuBO != null)
                {
                    this.tenTaiKhoan = "Tài khoản: " + taiKhoan + " - " + soHieuBO.Ten;
                    this.lblTenTK.Text = this.tenTaiKhoan;
                }
            }
        }
        #endregion control

        //=====================================
        #region button
        private void btnSearch_Click(object sender, EventArgs e)
        {
            FilterData();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            Print();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
            FilterData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        private void gridView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            int flag = StaticUtil.GetInt(this.gridView.GetRowCellValue(e.RowHandle, "Flag"), 0);

            if (flag != 0)
            {
                e.Appearance.Font = new Font("Tahoma", 8.25F, FontStyle.Bold);
                e.Appearance.ForeColor = Color.Blue;

                if (e.Column.FieldName == "DienGiai")
                    e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            }
        }
        #endregion grid

        //=====================================
        #region function
        private void FilterData()
        {
            Session.SoQuy_TaiKhoan = StaticUtil.GetString(this.lookUpTK.EditValue, null);
            if (string.IsNullOrEmpty(Session.SoQuy_TaiKhoan))
            {
                XtraMessageBox.Show("Chọn tài khoản", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            #region lay dieu kien
            this.tuNgay = this.dtThang.DateTime;

            int nam2 = this.tuNgay.Year;
            int thang2 = this.tuNgay.Month + 1;

            if (thang2 == 13)
            {
                thang2 = 1;
                nam2++;
            }

            string caption = "SỔ QUỸ " + this.tenTaiKhoan + " - THÁNG " + this.tuNgay.ToString("MM/yyyy");
            this.gridView.ViewCaption = caption;
            #endregion lay dieu kien


            string strTuNgay = "CONVERT(datetime,'" + this.tuNgay.ToString("yyyy-MM") + "-01 00:00:00',120)";
            string strDenNgay = "CONVERT(datetime,'" + nam2.ToString("0000") + "-" + thang2.ToString("00") + "-01 00:00:00',120)";

            //string where = "DaKhoa=0 AND NgayLapPhieu>=" + strTuNgay + " AND NgayLapPhieu<" + strDenNgay;
            string where = "NgayLapPhieu>=" + strTuNgay + " AND NgayLapPhieu<" + strDenNgay;
            where += " AND (No=N'" + Session.SoQuy_TaiKhoan + "' OR Co=N'" + Session.SoQuy_TaiKhoan + "')";

            SoQuyTienMatBOCollection soQuyTienMatBOCollection = SoQuyTienMatBOCollection.GetSoQuyTienMatBOCollection(where, "NgayLapPhieu,So");
            this.bds.DataSource = soQuyTienMatBOCollection;
            this.gridView.CheckLoaded();
            SetStt();

            this.btnPrint.Visible = (soQuyTienMatBOCollection.Count > 0);
        }

        private void RefreshData()
        {
            this.soHieuTaiKhoanBOCollection = SoHieuTaiKhoanBOCollection.GetSoHieuTaiKhoanBOCollection("SoHieu1<>131");
            this.soHieuTaiKhoanBOCollection.Sort();
            Utility.SetData(this.lookUpTK, this.soHieuTaiKhoanBOCollection, this.soHieuTaiKhoanBOCollection.Count);
            if (this.lookUpTK.EditValue == null)
                this.lookUpTK.EditValue = "111";
        }

        private void Print()
        {
            if (this.gridView.RowCount > 0)
            {
                Report.rpSoQuyTienMat report = new Report.rpSoQuyTienMat(this.tuNgay, this.tenTaiKhoan, (SoQuyTienMatBOCollection)this.bds.DataSource);
                report.ShowPreviewDialog();
            }
        }

        private void SetStt()
        {
            int stt = 0;
            int length = this.gridView.RowCount;
            int index = 0;
            for (int i = 0; i < length; i++)
            {
                index = this.gridView.GetVisibleRowHandle(i);
                if (index >= 0)
                    if (StaticUtil.GetInt(this.gridView.GetRowCellValue(index, "Flag"), 0) == 0)
                        this.gridView.SetRowCellValue(index, "STT0", ++stt);
            }
        }
        #endregion function
    }
}